package com.onlinelearn.chatservice.service;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Set;

@Service
public class SensitiveWordFilter {
    private Set<String> sensitiveWords;

    @PostConstruct
    public void loadSensitiveWords() {
        // 从数据库或配置文件加载敏感词
        sensitiveWords = loadWordsFromDatabase();
    }

    public String filter(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }

        String filteredContent = content;
        for (String word : sensitiveWords) {
            filteredContent = filteredContent.replace(word, "*".repeat(word.length()));
        }

        return filteredContent;
    }

    public void addSensitiveWord(String word) {
        sensitiveWords.add(word);
    }

    public void removeSensitiveWord(String word) {
        sensitiveWords.remove(word);
    }
} 